package sip

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"strings"
	"udp_tcp_client/sip/header"
	"udp_tcp_client/sip/header/common"
	"udp_tcp_client/util"
)

func SendPlaySpeedCmd(ip string, port int) (err error) {

	device := &Device{
		Id:       12,
		Ip:       ip,
		Port:     port,
		DeviceId: "32432",
	}

	broadcastXml := strings.Builder{}
	broadcastXml.WriteString("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n")
	broadcastXml.WriteString("<Notify>\n")
	broadcastXml.WriteString("<CmdType>Broadcast</CmdType>\n")
	broadcastXml.WriteString("<SN>17430</SN>\r\n")
	broadcastXml.WriteString("<SourceID>" + util.Conf.Sip.UserName + "</SourceID>\r\n")
	broadcastXml.WriteString("<TargetID>" + device.DeviceId + "</TargetID>\r\n")
	broadcastXml.WriteString("</Notify>\n")

	fromArgs := common.Args{}

	from, err := header.NewUserDomain(util.Conf.Sip.UserName, util.Conf.Sip.UserName, util.Conf.Sip.IP+util.COLON+fmt.Sprintf("%d", util.Conf.Sip.Port), fromArgs)
	if err != nil {
		return err
	}

	toArgs := common.Args{}

	to, err := header.NewUserDomain(device.DeviceId, device.DeviceId, device.Ip+util.COLON+fmt.Sprintf("%d", device.Port), toArgs)
	if err != nil {
		return err
	}

	message := &Message{}

	newMessageParam := NewMessageParam{
		util.MethodRegister, from, to,
		device.Ip,
		device.Port, util.ContentTypeMansrtsp, true, "", "32432", 1, "udp"}

	err = message.NewMessage(&newMessageParam)

	if err != nil {
		logrus.Infoln("[SendPlayPauseCmd] 构建消息体出现错误，错误信息：", err)
		return
	}

	err = util.NewRequest(message.String(), "udp", ip, port)

	if err != nil {
		return
	}
	logrus.Infoln("[SendPlayPauseCmd] 开始发送信令：", message.String())
	return
}
